define(['jquery'],function($) {
    $.fn.listHelper = function (options) {
        var opts = $.extend({activeCls: "active", item: "li",wrapper:$(this).find(".panel"),scrollTimes:3}, options);
        var me = $(this);
        var upKey = 38, downKey = 40;

        function bindUpDownKey(e) {
            var activeItem = me.find("." + opts.activeCls + ":first");
            //console.log(activeItem.next());return;
            if (!activeItem.length) {
                //$(document).unbind("keyup", bindUpDownKey);
                return;
            }
            //console.log(e);
            if(e.keyCode==upKey)
            {
               // activeItem.prev(opts.item+":first").trigger("click");
                var prev=activeItem.prev();
                if(prev.length>0)
                {
                    var itemHeight = activeItem.height();
                    //console.log(me.find(opts.wrapper));
                    //console.log((me.find(opts.wrapper).height()-next.position().top)+"==="+itemHeight);
                    // console.log(activeItem.position().top);
                    //console.log(prev.position().top-opts.wrapper.height());
                    //console.log(prev.position().top-opts.wrapper.height()+"==="+itemHeight);
                    //console.log(opts.wrapper.scrollTop());
                    //console.log(activeItem.parent().height()-prev.position().top);
                    if(prev.offset().top-opts.wrapper.offset().top<=itemHeight*2)
                    {
                        opts.wrapper.stop(true,true).animate({scrollTop:"-="+(itemHeight*opts.scrollTimes)});
                        //var scrollTop =opts.wrapper.scrollTop()-(itemHeight*opts.scrollTimes)||0;
                        //console.log(scrollTop);
                        //opts.wrapper.scrollTop(opts.wrapper.scrollTop()-(itemHeight*opts.scrollTimes));
                    }
                    prev.trigger("click");

                }
            }
            else if(e.keyCode==downKey)
            {
                //console.log(activeItem.next(opts.item+":last"));
                //console.log(activeItem.next());
                //console.log(activeItem.next());
                var next=activeItem.next();
                if(next.length>0)
                {
                    var itemHeight = activeItem.height();
                    //console.log(me.find(opts.wrapper));
                    //console.log((me.find(opts.wrapper).height()-next.position().top)+"==="+itemHeight);
                    if(next.offset().top-opts.wrapper.offset().top>=opts.wrapper.height()-itemHeight*2)
                    {
                        opts.wrapper.stop(true,true).animate({scrollTop:"+="+(itemHeight*opts.scrollTimes)});
                       // opts.wrapper.scrollTop((itemHeight*opts.scrollTimes)+opts.wrapper.scrollTop());
                    }
                    next.trigger("click");

                }

                //activeItem.nextAll(opts.item+":last").trigger("click");
            }
        }
            /*me.delegate(opts.item, "click", function () {
               // var item = $(this);
                //$(document).bind("keydown", bindUpDownKey);
            });*/
            $(document).bind("keydown", bindUpDownKey);
    }
});